package com.share.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.admin.mapper.UserMapper;
import com.share.admin.model.db.Role;
import com.share.admin.model.db.User;
import com.share.admin.model.vo.UserPageReq;
import com.share.admin.service.IRoleService;
import com.share.admin.service.IUserService;
import com.share.common.common.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户表  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-09-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private IRoleService roleService;

    @Override
    public User getByAccount(String account) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        return userMapper.selectOne(queryWrapper);
    }


    @Override
    public Set<User> listAll() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        List<User> users = userMapper.selectList(queryWrapper);

        Set<User> set = new HashSet<>();
        set.addAll(users);
        return set;
    }

    @Override
    public Set<User> listByUserId(Long userId) {
        User user = userMapper.selectById(userId);
        Set<User> set = new HashSet<>();
        set.add(user);
        return set;
    }

    @Override
    public Integer bindOrRemoveRoles(Boolean isBind, String userId, List<String> roleIds) {
        if (isBind != null && isBind) {
            return userMapper.bindRoles(userId, roleIds);
        } else {
            return userMapper.removeRoles(userId, roleIds);
        }
    }

    @Override
    public Page pageList(UserPageReq req) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getNickName())) {
            queryWrapper.like(User::getNickName, req.getNickName());
        }
        if (StringUtils.isNotBlank(req.getAccount())) {
            queryWrapper.like(User::getAccount, req.getAccount());
        }
        if (StringUtils.isNotBlank(req.getPhone())) {
            queryWrapper.like(User::getPhone, req.getPhone());
        }
        if (StringUtils.isNotBlank(req.getEmail())) {
            queryWrapper.like(User::getEmail, req.getEmail());
        }

        //用户信息分页
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<User> list = userMapper.selectList(queryWrapper);
        Page<User> page = new Page<>(list, new PageInfo<>(list).getTotal(), req);

        //查询用户的角色
        for (User user : list) {
            Long userId = user.getId();
            Set<Role> roles = roleService.listByUserId(userId);
            user.setRoles(roles);
        }
        return page;
    }
}
